<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <div class="shuxing">
        <el-form-item label="行政区划" prop="">
          <treeselect
            class="shuxing1"
            v-model="queryParams.areaId"
            :options="deptOptions"
            :normalizer="normalizer"
            placeholder="选择区域"
          />
        </el-form-item>
      </div>
      <el-form-item label="主体名称" prop="productName">
        <el-input
          v-model="queryParams.enterName"
          placeholder="请输入主体名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" type="primary" size="mini" @click="resetQuery"
          >重置</el-button
        >   <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ProducetHarvestInfo:harvestInfo:add']"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="harvestInfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="序号"
        align="center"
        type="index"
        :index="indexMethod"
        width="80"
      />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主体名称" align="center" prop="enterName" />
      <el-table-column label="基地名称" align="center" prop="baseName" />
      <el-table-column
        label="基地负责人"
        align="center"
        prop="baseChargePerson"
      />
      <el-table-column label="基地编号" align="center" prop="baseCode" />
      <el-table-column label="种植产品名称" align="center" prop="productName" />
      <el-table-column label="收货数量" align="center" prop="productNum" />
      <el-table-column label="单位" align="center" prop="noteTaker" />
      <el-table-column label="收货时间" align="center" prop="productDate" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ProducetHarvestInfo:harvestInfo:edit']"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ProducetHarvestInfo:harvestInfo:edit']"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品收获管理对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="60%"
      append-to-body
      class="add"
    >
      <div class="add-box">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="产品名称" prop="productName">
            <el-select v-model="form.productName" placeholder="请选择">
                <el-option
                  v-for="(item, index) in plantProcessInfoList"
                  :label="item.plantName"
                  :key="index"
                  :value="item.plantName"
                ></el-option>
              </el-select>
           
      
          </el-form-item>
          <el-form-item label="主体名称" prop="enterName">
            <el-input
              v-model="form1.enterName"
              placeholder="请输入主体名称"
              readonly
            />
          </el-form-item>
          <el-form-item label="基地名称" prop="baseName">
            <el-input
              v-model="form1.baseName"
              placeholder="请输入基地名称"
              readonly
            />
          </el-form-item>
          <el-form-item label="基地编号" prop="baseCode">
            <el-input
              v-model="form1.baseCode"
              placeholder="请输入基地编号"
              readonly
            />
          </el-form-item>
          <el-form-item label="基地负责人" prop="baseChargePerson">
            <el-input
              v-model="form1.baseChargePerson"
              placeholder="请输入基地负责人"
              readonly
            />
          </el-form-item>
          
            <el-form-item label="行政区划" prop="">
              <treeselect 
                class="shuxing1"
                v-model="form1.areaId1"
                :options="deptOptions"
                :normalizer="normalizer"
                placeholder="选择区域"
              />
            </el-form-item>
          
          <el-form-item label="收货方式" prop="noteTaker">
            <el-input v-model="form.productMode" placeholder="请输入收货方式" />
          </el-form-item>
          <el-form-item label="收货数量" prop="mainBodyId">
            <el-input v-model="form.productNum" placeholder="请输入收货数量" />
          </el-form-item>
          <el-form-item label="单位">
            <el-select v-model="form.productUnit" placeholder="请选择单位">
              <el-option label="千克" value="农资店" />
              <el-option label="克" value="兽药店" />
              <el-option label="斤" value="种植业主体" />
              <el-option label="吨" value="畜牧业主体" />
              <el-option label="粒" value="渔业主体" />
            </el-select>
          </el-form-item>
          <el-form-item label="收货时间" prop="">
            <div class="block">
              <el-date-picker
                v-model="form.otime"
                type="date"
                placeholder="选择日期"
              >
              </el-date-picker>
            </div>
          </el-form-item>

          <el-form-item label="产品批次码" prop="productBatchCode">
            <el-input
              v-model="form.productBatchCode"
              placeholder="系统自动生成"
              disabled
            />
          </el-form-item>
          <el-form-item label="记录人" prop="baseId">
            <el-input v-model="form.noteTaker" placeholder="请输入基地编号" />
          </el-form-item>
           <el-form-item >
          </el-form-item>
          <el-form-item label="主体照片" prop="baseId">
            <div v-for="(item, index) in form1.list" :key="index" class="photo">
              <img :src="item" />
            </div>
          </el-form-item>
          <el-form-item> </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <div class="button">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listHarvestInfo,
  getHarvestInfo,
  delHarvestInfo,
  addHarvestInfo,
  updateHarvestInfo,
  exportHarvestInfo,
  getTree,
} from "@/api/ProducetHarvestInfo/harvestInfo";
import { listPlantProcessInfo } from "@/api/TPlantProcessInfo/plantProcessInfo";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "HarvestInfo",
  components: { Treeselect },
  watch:{

     "form.productName": function () {
      for (let i = 0; i < this.plantProcessInfoList.length; i++) {
        if (this.plantProcessInfoList[i].plantName == this.form.productName) {
          this.form1 = this.plantProcessInfoList[i];
          this.form1.areaId1=this.plantProcessInfoList[i].areaId;
          this.form.baseId=this.plantProcessInfoList[i].baseId;
          let arr=this.plantProcessInfoList[i].subjectIntroduce.subjectIntroduceImg;
          this.form1.list=arr.split(",");
        }
        else{
          this.form1=[];
        }
      }
    },

  },
  data() {
    return {
      form1:[],
      list: [
        "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
        "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
        "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
      ],
      //
      value: "",
      // 树状数
      deptOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 产品收获管理表格数据
      harvestInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      plantProcessInfoList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: undefined,
        productMode: undefined,
        productNum: undefined,
        productUnit: undefined,
        productDate: undefined,
        noteTaker: undefined,
        mainBodyId: undefined,
        baseId: undefined,
      },
      // 表单参数
      form: {
        otime: "",
      },
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.getTree();
  },
  methods: {
    /** 查询产品收获管理列表 */
    getList() {
      this.loading = true;
      listHarvestInfo(this.queryParams).then((response) => {
        this.harvestInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 添加页面中的查询
    select() {},
    // 行政区划分列表
    getTree() {
      this.loading = true;
      getTree().then((response) => {
        this.deptOptions = this.handleTree(response.data, "id");
      });
    },
    getList11() {
      listPlantProcessInfo(this.queryParams).then((response) => {
        console.log(response.rows);
        this.plantProcessInfoList = response.rows;
      });
    },
    // 序号
    indexMethod(index) {
      return index + 1;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        productName: undefined,
        productMode: undefined,
        productNum: undefined,
        productUnit: undefined,
        productDate: undefined,
        noteTaker: undefined,
        mainBodyId: undefined,
        baseId: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams={
        pageNum: 1,
        pageSize: 10,
        productName: undefined,
        productMode: undefined,
        productNum: undefined,
        productUnit: undefined,
        productDate: undefined,
        noteTaker: undefined,
        mainBodyId: undefined,
        baseId: undefined,
      }
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      
     this.getList11();

      this.open = true;
      this.title = "添加产品收获管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      
     this.getList11();
      this.reset();
      const id = row.id || this.ids;
      getHarvestInfo(id).then((response) => {
        this.form1=response.data;
        this.form=response.data;
        this.form.productBatchCode=response.data.productBatchCode;
        this.form.otime=response.data.productDate;
        this.form1.areaId1=response.data.areaId;
        this.open = true;
        this.title = "修改产品收获管理";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
         let otime = new Date(this.form.otime);
            let oyear = otime.getFullYear();
            let omouth = otime.getMonth() + 1;
            let oday = otime.getDate();
            let otime1 = oyear + "-" + omouth + "-" + oday;
            this.form.productDate=otime1;
        if (valid) {
          if (this.form.id != undefined) {
            updateHarvestInfo(this.form).then((response) => {
              if (response.code == 0) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            
            addHarvestInfo(this.form).then((response) => {
              if (response.code == 0) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除产品收获管理编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delHarvestInfo(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有产品收获管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportHarvestInfo(queryParams);
        })
        .then((response) => {
          this.download(response.data);
        })
        .catch(function () {});
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.label,
        children: node.children,
      };
    },
  },
};
</script>
<style lang="scss" scoped>
.shuxing {
  width: 250px;
  float: left;
  .shuxing1 {
    width: 150px !important;
    height: 35px;
  }
}
.add-box .el-form-item {
  width: 47%;
}
.add-box {
  .el-form {
    width: 100%;
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
  }
}
.el-form-item__content {
  width: 100%;
  .el-select--medium {
    width: 100%;
  }
}
.el-date-editor {
  width: 100%;
}
.photo {
  width: 100%;
  img {
    width: 100px;
    height: 100px;
    float: left;
    margin-left: 5px;
  }
}
.dialog-footer{
  width: 100%;
  height: 30px;
  .button{
    float: right;
    margin-right: 5px;
  }
}
</style>